package com.thiswhyme.mq.kafka.consumer2.consumer;

import com.thiswhyme.mq.kafka.common.consts.TopicNameDefinition;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

/**
 * @description: 消费者
 * @author: thiswhyme
 * @date: 2021/4/21
 */
@Slf4j
@Component
public class KafkaConsumer {

    private static final String BIZ_GROUP_ID = "kafka-consumer-group-biz1";

    /**
     * 监听普通消息1（手动确认）
     * @param record
     */
    @KafkaListener(topics = {TopicNameDefinition.COMMON_TOPIC}, containerFactory = "manualAckContainerFactory")
    public void onCommonMessage1(ConsumerRecord<?, ?> record, Acknowledgment acknowledgment){
        log.info("=====【onCommonMessage1】监听到消息，topic：{}，partition：{}，内容：{}", record.topic(),
                record.partition(), record.value());
        acknowledgment.acknowledge();
    }

    /**
     * 监听普通消息2
     * @param record
     */
    @KafkaListener(topics = {TopicNameDefinition.COMMON_TOPIC2})
    public void onCommonMessage2(ConsumerRecord<?, ?> record){
        log.info("=====【onCommonMessage2】监听到消息，topic：{}，partition：{}，内容：{}", record.topic(),
                record.partition(), record.value());
    }

    /**
     * 监听普通消息2
     * @param record
     */
    @KafkaListener(topics = {TopicNameDefinition.COMMON_TOPIC2}, groupId = BIZ_GROUP_ID)
    public void onCommonMessage3(ConsumerRecord<?, ?> record){
        log.info("=====【biz-group】【onCommonMessage2】监听到消息，topic：{}，partition：{}，内容：{}", record.topic(),
                record.partition(), record.value());
    }

    /**
     * 监听可过滤消息
     * @param record
     */
    @KafkaListener(topics = {TopicNameDefinition.FILTER_TOPIC}, containerFactory = "filterContainerFactory")
    public void onFilterMessage(ConsumerRecord<?, ?> record) {
        log.info("=====【onFilterMessage】监听到消息，内容：{}", record.value());
    }
}